package com.yanqu.backendweb.utils;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;
import com.yanqu.backendweb.bean.GameProp;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.MD5Security;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @author duli
 * @version 1.0
 * @date 2020/9/23 10:09
 * @Description 通用工具类
 */
public class Utils {
    private final static Logger log = LoggerFactory.getLogger(Utils.class);
    private static final String COMMA = ",";

    public static String concat(Collection<? extends Serializable> valueList) {
        return concat(COMMA, valueList.toArray());
    }

    public static String concat(Object... values) {
        return concat(COMMA, values);
    }

    public static String concat(String separator, Collection<? extends Serializable> valueList) {
        return concat(separator, valueList.toArray());
    }

    public static String concat(String separator, Object... values) {
        Assert.isTrue(separator != null, "separator 不能为空");
        if (ArrayUtils.isEmpty(values)) {
            return null;
        }
        return StringUtils.join(values, separator);
    }
    public static <T> Set<T> toSpecifySet(String value, Class<T> type) {
        return toSpecifyCollector(value, COMMA, type, Collectors.toSet());
    }
    public static <T> Set<T> toSpecifySet(String value, String separator, Class<T> type) {
        return toSpecifyCollector(value, separator, type, Collectors.toSet());
    }

    public static <T> List<T> toSpecifyList(String value, Class<T> type) {
        return toSpecifyCollector(value, COMMA, type, Collectors.toList());
    }
    public static <T> List<T> toSpecifyList(String value, String separator, Class<T> type) {
        return toSpecifyCollector(value, separator, type, Collectors.toList());
    }
    public static <C, T> C toSpecifyCollector(String value, String separator, Class<T> type, Collector<T, ?, C> collector) {
        Assert.isTrue(separator != null, "separator 不能为空");
        if (StringUtils.isEmpty(value)) {
            return null;
        }

        String[] array = value.split(separator);
        return Arrays.stream(array).map(i -> TypeUtils.cast(i, type, ParserConfig.getGlobalInstance())).
                collect(collector);
    }
    public static String getSign(SortedMap<String, String> paramMap, String appSecret){
        String sign = "";
        if(com.yanqu.road.utils.string.StringUtils.isNullOrEmpty(appSecret)){
            return sign;
        }
        StringBuffer sb = new StringBuffer();
        for(Map.Entry<String, String> dataEntry : paramMap.entrySet()){
            String key = dataEntry.getKey();
            String value = dataEntry.getValue();
            if(!com.yanqu.road.utils.string.StringUtils.isNullOrEmpty(value) && !"sign".equals(key)){
                sb.append(key + "=" + value + "&");
            }
        }
        if(sb.length() > 0){
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(appSecret);
        try {
            log.info("sign pre value:{}", sb.toString());
            sign = MD5Security.md5(sb.toString()).toLowerCase();
            log.info("sign value:{}", sign);
        } catch (Exception e) {
            log.error("getSign error:", e);
        }
        return sign;
    }

    public static boolean checkSign(SortedMap<String, Object> paramMap, String sign) {
        String mySign = Utils.getSignObject(paramMap, ConfigHelper.getValue("gm_api_secret"));
        if(mySign.equals(sign)){
            return true;
        }else{
            return false;
        }
    }
    public static String getSignObject(SortedMap<String, Object> paramMap, String appSecret){
        String sign = "";
        if(com.yanqu.road.utils.string.StringUtils.isNullOrEmpty(appSecret)){
            return sign;
        }
        StringBuffer sb = new StringBuffer();
        for(Map.Entry<String, Object> dataEntry : paramMap.entrySet()){
            String key = dataEntry.getKey();
            String value = String.valueOf(dataEntry.getValue());
            if(!com.yanqu.road.utils.string.StringUtils.isNullOrEmpty(value) && !"sign".equals(key)){
                sb.append(key + "=" + value + "&");
            }
        }
        if(sb.length() > 0){
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(appSecret);
        try {
            log.info("sign pre value:{}", sb.toString());
            sign = MD5Security.md5(sb.toString()).toLowerCase();
            log.info("sign value:{}", sign);
        } catch (Exception e) {
            log.error("getSign error:", e);
        }
        return sign;
    }


    public static String getRewardEscape(Map<Integer, GameProp> gamePropsMap, String reward) {
        StringBuffer sb = new StringBuffer();
        if(!reward.isEmpty()){
           Set<String> rewardSet= toSpecifySet(reward,";",String.class);
           for(String rew:rewardSet){
               Integer goodId=Integer.valueOf(rew.substring(0,rew.indexOf("=")));
               String num=rew.substring(rew.indexOf("=")+1,rew.length());
               GameProp prop=gamePropsMap.get(goodId);
               if(prop!=null){
                   sb.append(prop.getName()+"="+num+";");
               }
           }
        }
        if(sb.length() > 0){
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
